﻿
namespace CrmClient.QueryConvert
{
    internal class CrmAttrName
    {
        public bool IsAliased { get; private set; }

        public bool IsId { get; private set; }

        public string Name { get; private set; }

        public string Alias { get; private set; }

        public CrmAttrName(string name, bool isId)
        {
            IsId = isId;
            Name = name;
        }

        public CrmAttrName(string alias, string name, bool isId)
            : this(name, isId)
        {
            Alias = alias;
            IsAliased = true;
        }

        public override int GetHashCode()
        {
            if (IsAliased)
                return unchecked(23 * Alias.GetHashCode() * Name.GetHashCode());
            return Name.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            bool res = false;
            if (obj is CrmAttrName)
                res = ((CrmAttrName)obj).Name == this.Name && ((CrmAttrName)obj).Alias == this.Alias;
            return res;
        }

        public override string ToString()
        {
            if (IsAliased)
                return string.Format(Alias + "." + Name);
            return Name;
        }
    }
}
